package com.linq.cool.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.linq.cool.api.domain.SysOperateLog;
import com.linq.cool.common.core.domain.BaseController;
import com.linq.cool.common.core.domain.PageVO;
import com.linq.cool.common.core.domain.Result;
import com.linq.cool.common.core.utils.ExcelUtils;
import com.linq.cool.common.core.utils.SecurityUtils;
import com.linq.cool.common.log.annotation.Log;
import com.linq.cool.common.log.enums.BusinessType;
import com.linq.cool.common.security.annotation.PreAuthorize;
import com.linq.cool.system.dto.SysOperateLogQueryDTO;
import com.linq.cool.system.service.SysOperateLogService;
import com.linq.cool.system.vo.SysOperateLogVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;


/**
 * @Author:linq
 * @Date: 2021-01-27
 * @Description: 操作日志记录Controller
 * @Version: 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("operateLog")
public class SysOperateLogController extends BaseController {
    @Autowired
    private SysOperateLogService sysOperateLogService;

    /**
     * 条件分页条件获取操作日志记录列表
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operateLog:list")
    @GetMapping("list")
    public Result<?> list(SysOperateLogQueryDTO dto) {
        log.info("SysOperateLogQueryDTO:{}", dto);
        IPage<SysOperateLogVO> page = sysOperateLogService.listByPage(dto.getPage(), dto);
        return Result.ok(new PageVO().setTotal(page.getTotal()).setRecords(page.getRecords()));
    }

    /**
     * 获取操作日志记录详细信息
     *
     * @param id
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operateLog:query")
    @GetMapping("{id}")
    public Result<?> getInfo(@PathVariable("id") Long id) {
        return Result.ok(sysOperateLogService.getById(id));
    }


    /**
     * 新增操作日志记录
     *
     * @param sysOperateLog SysOperateLog
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operateLog:add")
    @PostMapping
    public Result<?> add(@RequestBody SysOperateLog sysOperateLog) {
        sysOperateLog.setCreator(SecurityUtils.getUserId());
        return this.toResult(sysOperateLogService.save(sysOperateLog));
    }

    /**
     * 修改操作日志记录
     *
     * @param sysOperateLog SysOperateLog
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operateLog:edit")
    @PutMapping
    public Result<?> edit(@RequestBody SysOperateLog sysOperateLog) {
        sysOperateLog.setModifier(SecurityUtils.getUserId()).setGmtModified(new Date());
        return this.toResult(sysOperateLogService.updateById(sysOperateLog));
    }

    /**
     * 批量删除操作日志记录
     *
     * @param ids List<Long> ID集合
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operateLog:remove")
    @Log(moduleTitle = "操作日志记录", businessType = BusinessType.DELETE)
    @DeleteMapping("{ids}")
    public Result<?> remove(@PathVariable("ids") List<Long> ids) {
        return this.toResult(sysOperateLogService.removeByIds(ids));
    }


    /**
     * 导出操作日志记录列表
     *
     * @param sysOperateLog SysOperateLog
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operateLog:export")
    @Log(moduleTitle = "操作日志记录", businessType = BusinessType.EXPORT)
    @PostMapping("exportExcel")
    public void exportExcel(HttpServletResponse response, SysOperateLog sysOperateLog) throws IOException {
        List<SysOperateLog> list = sysOperateLogService.listByParams(sysOperateLog);
        ExcelUtils<SysOperateLog> utils = new ExcelUtils<>(SysOperateLog.class);
        utils.exportExcel(response, list, "log");
    }

    /**
     * 清空操作日志
     *
     * @return 结果
     */
    @PreAuthorize(hasPerm = "sys:operlog:remove")
    @Log(moduleTitle = "操作日志", businessType = BusinessType.CLEAN)
    @DeleteMapping("clean")
    public Result<?> clean() {
        sysOperateLogService.clean();
        return Result.ok();
    }

}
